﻿using DTCash.Logger;
using DTCash.Logger.DTO;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace Uranus.Common
{
    public class ConfigCommon
    {

        private static string config;

        public static List<OperationConfigDTO> getConfig(string operation)
        {
            try
            {
                if (string.IsNullOrEmpty(config))
                {
                    config = ConfigurationManager.AppSettings["ConfigCenterUrl"];
                }
                string ConfigCenterUrl = config + "&operation=" + operation;
                string str = Uranus.Common.RequestCommon.SimulationHttp(ConfigCenterUrl, "", "get");
                NodeResponse nodeResponse = new JavaScriptSerializer().Deserialize<NodeResponse>(str);
                if (nodeResponse != null && nodeResponse.state == 1)
                {
                    return new JavaScriptSerializer().Deserialize<List<OperationConfigDTO>>(new JavaScriptSerializer().Serialize(nodeResponse.data));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error,"获取配置中心配置出现异常", ex.ToString());
            }
            return null;
        }
        public static OperationConfigDTO getConfigByOne(string operation)
        {
            try
            {
                if (string.IsNullOrEmpty(config))
                {
                    config = ConfigurationManager.AppSettings["ConfigCenterUrl"];
                }
                string ConfigCenterUrl = config + "&operation=" + operation;
                string str = Uranus.Common.RequestCommon.SimulationHttp(ConfigCenterUrl, "", "get");
                NodeResponse nodeResponse = new JavaScriptSerializer().Deserialize<NodeResponse>(str);
                if (nodeResponse != null && nodeResponse.state == 1)
                {
                    return new JavaScriptSerializer().Deserialize<OperationConfigDTO>(new JavaScriptSerializer().Serialize(nodeResponse.data));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取配置中心配置出现异常", ex.ToString());
            }
            return null;
        }
    }

    [Serializable]
    public class NodeResponse
    {
        public int state { get; set; }
        public object data { get; set; }
    }

    public class OperationConfigDTO
    {
        public int operationtype { get; set; }

        public int operationstatus { get; set; }

        public string desc { get; set; }

        public object value { get; set; }

        public DateTime createtime { get; set; }

        public string starttime { get; set; }

        public string endtime { get; set; }
    }
}
